import hashlib

from django.core.paginator import Paginator
from django.shortcuts import render
from django.http import HttpResponse
from common.models import Member,Orders

## 会员管理首页
#  列出会员列表
#  会员指手机终端用户
def index(request, pIndex):
    """会员管理首页"""
    list = Member.objects.filter(status__lt = 9)
    # 分页显示
    num_per_page = 5
    p = Paginator(list, num_per_page - 1)  # 显示5项
    print("pIndex:" + pIndex)
    if pIndex == "":
        pIndex = 1
    clist = p.page(pIndex)
    alist = p.page_range
    # 传入前n页
    # 传入后n页
    # 如果是上次显示的最后一页则加载下n页
    print(clist)
    context = {"userslist": clist, "alist": alist, "pIndex": int(pIndex), "pagetype": int(1),
               "urlname": "myadmin_member"}
    return render(request, "myadmin/member/member.html", context)

## 更新会员信息
#  无密码
def update(request):
    """编辑更新会员信息"""
    try:
        ob = Member.objects.get(id=int(request.POST['uid']))
        ob.nickname = request.POST['nickname']
        ob.mobile = request.POST['mobile']
        ob.status = int(request.POST['status'])
        
        
        # 图片的上传处理
        myfile = request.FILES.get("pic",None)
        if not myfile:
            return HttpResponse("没有上传文件信息")
        #filename = str(time.time())+"."+myfile.name.split('.').pop()
        filename = ob.nickname+"."+str(time.time())
        destination = open("./static/member/"+filename,"wb+")
        for chunk in myfile.chunks():      # 分块写入文件  
            destination.write(chunk)  
        destination.close()

        # 图片的缩放
        im = Image.open("./static/member/"+filename)
        # 缩放到375*375(缩放后的宽高比例不变):
        im.thumbnail((375, 375)) 
        im.save("./static/member/"+filename,None)
        
        im = Image.open("./static/member/"+filename)
        # 缩放到220*220(缩放后的宽高比例不变):
        im.thumbnail((110,110)) 
        im.save("./static/member/m_"+filename,None)

        im = Image.open("./static/member/"+filename)
        # 缩放到75*75(缩放后的宽高比例不变):
        im.thumbnail((25, 25)) 
        im.save("./static/member/s_"+filename,None)

        ob.save()
        context = {"info": "修改成功！", "reason":''}
    except Exception as err:
        context = {"info": "修改失败", "reason":err}
    return render(request, "myadmin/info.html", context)


def orders(request): 
    """用户订单查询"""
    order_status = {1:'过行中', 2:'无效', 3:'已完成'}
    payment_status = {1:'未支付', 2:'已支付', 3:'已退款'}
    try:
        uid = int(request.POST['uid'])
        print("[DEBUG] member.orders uid = %d" % uid)
        orders = Orders.objects.filter(member_id__exact = request.POST['uid'])
        context = ""
        context = "<table class=\"table table-striped\">"+\
            "<thead><tr>"+\
            "<th>订单id</th>"+\
            "<th>店铺id</th>"+\
            "<th>会员id</th>"+\
            "<th>操作员id</th>"+\
            "<th>金额</th>"+\
            "<th>订单状态</th>"+\
            "<th>支付状态</th>"+\
            "<th>添加时间</th>"+\
            "<th>修改时间</th>"+\
            "</tr></thead><tbody>"
        for od in orders: 
            
            context = context + "<tr>"+\
            "<th>"+str(od.id)+"</th>"+\
            "<td>"+str(od.shop_id)+"</td>"+\
            "<td>"+str(od.member_id)+"</td>"+\
            "<td>"+str(od.user_id)+"</td>"+\
            "<td>"+str(od.money)+"</td>"+\
            "<td>"+order_status[od.status]+"</td>"+\
            "<td>"+payment_status[od.payment_status]+"</td>"+\
            "<td>"+str(od.create_at)+"</td>"+\
            "<td>"+str(od.update_at)+"</td>"+\
            "</tr>"
        context = context + "</tbody></table>"
        print("[DEBUG] context = %s" % context)
    except Exception as err:
        context = "查询失败"
    return HttpResponse(context);
  
def memdel(request): 
    """删除会员 状态级删除"""
    try:
        uid = int(request.POST['uid'])
        print("删除 id=%d" % uid)
        ob = Member.objects.get(id=uid)
        ob.status = 9
        ob.save()
        #context = {"info": "删除成功！", "reason":''}
        return HttpResponse("删除成功！")
    except Exception as err:
        context = {"info": "删除失败", "reason":err}
        return HttpResponse("删除失败！")